///<reference path='SPRESTList.ts'/>
///<reference path='SPRESTContext.ts'/>
///<reference path='SPRESTWeb.ts'/>
///<reference path='SPRESTFileCollection.ts'/>

// Module
module SPREST {

    // Class
    export class Folder {
        public urlFiles: string = null;
        public urlListItemAllFields: string = null;
        public urlParentFolder: string = null;
        public urlProperties: string = null;
        public urlFolders: string = null;
        public itemCount: number = null;
        public name: string = null;
        public serverRelativeUrl: string = null;
        public welcomePage: string = null;

        public started: bool = false;
        public ready: bool = false;
        public dfd: JQueryDeferred = $.Deferred();
        constructor (public Context: SPREST.Context, public Web:SPREST.Web, public apiUrl: string) {

        }

        start(): JQueryDeferred {
            var myThis = this;
            if (myThis.started) return myThis.dfd;
            myThis.started = true;
            $.ajax(myThis.apiUrl,
               {
                async: true,
                headers: { "Accept":"application/json;odata=verbose" },
                type: "GET"
            }
            ).fail(function (req, status, error) {
                myThis.dfd.reject(myThis, req, status, error);
            }).done(function (data) {
                myThis.urlFiles = data.d.Files.__deferred.uri;
                myThis.urlListItemAllFields = data.d.ListItemAllFields.__deferred.uri;
                myThis.urlParentFolder = data.d.ParentFolder.__deferred.uri;
                myThis.urlProperties = data.d.Properties.__deferred.uri;
                myThis.urlFolders = data.d.Folders.__deferred.uri;
                myThis.itemCount = data.d.ItemCount;
                myThis.name = data.d.Name;
                myThis.serverRelativeUrl = data.d.ServerRelativeUrl;
                myThis.welcomePage = data.d.WelcomePage;
                myThis.ready = true;
                myThis.dfd.resolve(myThis);
            });
            return myThis.dfd;
        }

        getFiles(): SPREST.FileCollection {
            var files = new SPREST.FileCollection(this.Context, this.Web, this, this.urlFiles);
            files.start();
            return files;
        }
    }   

}
